// merchants/holderProfits/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    isHolder: 0,
    profits: {},
    recordList: [],
    shareUid: 0
  },

  toSetAccount: function(){
    var url = 'setAccount.html';
    wx.navigateTo({
      url: '/pages/h5page/index?url=' + url,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.isHolder) {
      this.setData({
        isHolder: options.isHolder
      })
    }
    if (options.shareUid) {
      this.setData({
        shareUid: options.shareUid
      })
    }
    if (options.creatUid) {
      this.getBrokerageTotal(options.creatUid);
    }
    if (!options.isHolder){
      this.getBrokerageRecordList(options.shareUid);
    }

  },

  // 获取我的收益
  getBrokerageTotal: function (creatUid) {
    let that = this;
    let uid = app.globalData.userInfo.Uid;
    if (!that.data.isHolder){
      uid = that.data.shareUid;
    }
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetBrokerageTotal?showOut=1&showOrder=1&createUid=' + creatUid + '&uid=' + uid,
      success: function(res) {
        if (res.data.Code == 0) {
          let profits = {};
          if (res.data.Data != null) {
            profits = res.data.Data;
            profits.sumProfits = (profits.OutAmount + profits.Balance).toFixed(2);
          }
          that.setData({
            profits: profits
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  toTixian: function() { //提现
    wx.navigateTo({
      url: '/fenxiao/tixian/index?isHolder=1&balance=' + this.data.profits.Balance + '&creatUid=' + this.data.profits.CreateUid,
    })
  },

  // 股东分成记录(商家查看)
  getBrokerageRecordList: function (shareUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetBrokerageRecordList',
      data: {
        createUid: app.globalData.userInfo.Uid,
        shareUid: shareUid,
        direction: 1,
        page: that.data.CurrentPage
      },
      success: function (res) {
        if (res.data.Code == 0) {
          var recordList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.recordList
            recordList = preGoods.concat(recordList);
          }
          for (let i = 0; i < recordList.length; i++) {
            recordList[i].Ydate = recordList[i].CreateTime.substr(0, 10);
            recordList[i].Htime = recordList[i].CreateTime.substr(11, 8);
          }
          that.setData({
            recordList: recordList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getBrokerageRecordList(that.data.shareUid);
    } else {
      return
    }
  },

})